﻿Imports System.Text.RegularExpressions
Imports System.ComponentModel
Imports OnlineVideos.Sites.WebSite
Imports MediaPortal.GUI.Library
Imports MediaPortal.Dialogs

Public Class TVALaCartaUtil
    Inherits SiteUtilBase

    Private site As WebSite

    Public Overrides Function DiscoverDynamicCategories() As Integer
        Dim webData = GetWebData("http://blog.tvalacarta.info/canales/")

        If Settings.Categories Is Nothing Then Settings.Categories = New BindingList(Of Category)

        Dim m = Regex.Match(webData, "<h3>(?<title>[^<]*)</h3>")
        While m.Success
            Dim newCat = New RssLink()
            newCat.Name = m.Groups("title").Value
            newCat.Other = Sections.MainWebSite

            If newCat.Name.StartsWith("Cadenas") Then
                Settings.Categories.Add(newCat)
                SetChannelsForCategory(webData, newCat)
            End If

            m = m.NextMatch()
        End While

        Settings.DynamicCategoriesDiscovered = (Settings.Categories.Count > 0)

        Return Settings.Categories.Count
    End Function

    Private Sub SetChannelsForCategory(ByVal webData As String, parentCategory As RssLink)
        Dim catEntry = "<h3>" + parentCategory.Name + "</h3>"
        Dim p1 = webData.IndexOf(catEntry) + catEntry.Length
        Dim p2 = webData.IndexOf("<h3>", p1)
        webData = webData.Substring(p1, p2 - p1)

        parentCategory.SubCategories = New List(Of Category)()

        Dim m = Regex.Match(webData, "<a href=""(?<url>[^<]*)""><img src=""(?<thumb>[^<]*)""><span class=""titulo"">(?<title>[^<]*)</span>")
        While m.Success
            Dim newChannel = New RssLink()
            newChannel.Name = m.Groups("title").Value
            newChannel.Thumb = m.Groups("thumb").Value
            newChannel.Url = m.Groups("url").Value
            newChannel.Other = Sections.InChannel
            newChannel.HasSubCategories = True

            parentCategory.SubCategories.Add(newChannel)

            m = m.NextMatch()
        End While

        If parentCategory.SubCategories.Count > 0 Then
            parentCategory.HasSubCategories = True
            parentCategory.SubCategoriesDiscovered = True
        End If
    End Sub

    Public Overrides Function DiscoverSubCategories(parentCategory As Category) As Integer
        Select Case CType(parentCategory.Other, Sections)
            Case Sections.MainWebSite
                site = Nothing
            Case Sections.InChannel
                Dim newSite As WebSite = Nothing

                ' TODO: This is kind of dumb. Future versions should use Reflection to find the appropriate site.
                Select Case CType(parentCategory, RssLink).Url
                    Case "http://www.americatv.com.pe/portal/multimedia"
                        newSite = New AmericaTV()
                    Case "http://www.antena3.com/videos/"
                        newSite = New Antena3()
                    Case "http://play.tuteve.tv/"
                        newSite = New ATV()
                End Select

                If newSite IsNot Nothing AndAlso site <> newSite Then
                    site = newSite
                    parentCategory.SubCategories = site.GetCategories()
                    parentCategory.HasSubCategories = (parentCategory.SubCategories.Count > 0)
                End If
            Case Else
                parentCategory.SubCategories = site.GetSubCategories(parentCategory)
                parentCategory.HasSubCategories = (parentCategory.SubCategories.Count > 0)
        End Select

        If site Is Nothing Then
            ' TODO: Try to be more gentle with this message. Need to find out how to display an actual Message Box.
            Throw New OnlineVideosException("Support for this site not available yet." + vbCrLf + "Hopefully, it will be added soon!")
            Return 0
        Else
            parentCategory.SubCategories.ForEach(Sub(cat) cat.ParentCategory = parentCategory)
            parentCategory.SubCategoriesDiscovered = True

            Return parentCategory.SubCategories.Count
        End If
    End Function

    Public Overrides Function getVideoList(category As Category) As System.Collections.Generic.List(Of VideoInfo)
        Return site.GetVideos(category)
    End Function

    Public Overrides Function getUrl(video As VideoInfo) As String
        Return site.GetVideoURL(video)
    End Function

    Public Shared Function CombinePath(p1 As String, p2 As String) As String
        If p1.Contains("/") Then
            ' TODO: Is there a native .NET function to combine web paths?
            If p2.StartsWith(p1) Then
                Return p2
            Else
                If Not p1.EndsWith("/") Then p1 += "/"
                If p2.StartsWith("/") Then p2 = p2.Substring(1)
                Return p1 + p2
            End If
        Else
            Return IO.Path.Combine(p1, p2)
        End If
    End Function
End Class
